package com.atguigu.userservice.controller;


import cn.hutool.core.convert.Convert;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.utils.LoginUser;
import com.atguigu.userservice.entity.UcenterMember;
import com.atguigu.userservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Mr.Alan
 * @since 2020-08-27
 */
@RestController
@RequestMapping("/userservice/ucenter-member")
public class UcenterMemberController {
    @Resource
    private RedisTemplate<String, String> jsonRedisTemplate;
    @Resource
    private UcenterMemberService ucenterMemberService;

    /**
     * 注册会员
     */
    @ApiOperation("注册会员")
    @PostMapping("/register")
    public R register(@RequestBody UcenterMember ucenterMember) {

        String errorMsg = checkRegisterParam(ucenterMember);
        if (StringUtils.isNotEmpty(errorMsg)) {
            return R.error(errorMsg);
        }
        ucenterMemberService.register(ucenterMember);
        return R.ok("注册成功");

    }

    /**
     * 检验注册的参数是否正确
     */
    private String checkRegisterParam(UcenterMember ucenterMember) {

        if (StringUtils.isEmpty(ucenterMember.getMobile()) || StringUtils.isEmpty(ucenterMember.getPassword())) {
            return "手机号和密码密码不能为空";
        }
        if (StringUtils.isEmpty(ucenterMember.getCode())) {
            return "验证码不能为空";
        }
        Integer count = ucenterMemberService.lambdaQuery().eq(UcenterMember::getMobile, ucenterMember.getMobile()).count();
        if (count != 0) {
            return "手机号已经被注册";
        }
        String code = Convert.toStr(jsonRedisTemplate.opsForValue().get(String.format("sms:phone:register:%s", ucenterMember.getMobile())));
        if (!ucenterMember.getCode().equals(code)) {
            return "验证码不正确,请重新输入";
        }
        jsonRedisTemplate.delete(String.format("sms:phone:register:%s", ucenterMember.getMobile()));
        return null;
    }

    /**
     * 使用账号密码登录
     */
    @ApiOperation("使用账号密码登录")
    @PostMapping("/login")
    public R login(@RequestBody UcenterMember ucenterMember) {

        String errorMsg = checkLoginParam(ucenterMember);
        if (StringUtils.isNotEmpty(errorMsg)) {
            return R.error(errorMsg);
        }

        String token = ucenterMemberService.login(ucenterMember);
        if (StringUtils.isNotEmpty(token)) {
            return R.ok().data("token", token);
        }
        return R.error("登录失败");
    }

    /**
     * 检验登录的参数是否正确
     */
    private String checkLoginParam(UcenterMember ucenterMember) {

        if (StringUtils.isEmpty(ucenterMember.getMobile()) || StringUtils.isEmpty(ucenterMember.getPassword())) {
            return "手机号和密码密码不能为空";
        }
        return null;
    }

    /**
     * 退出登录
     */
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public R logout() {
        if (ucenterMemberService.logout()) {
            return R.ok("退出成功");
        }
        return R.error("退出失败");
    }

    /**
     * 从token中获取信息
     */
    @ApiOperation("从token中获取信息")
    @PostMapping("/getInfo")
    public R getInfo() {
        LoginUser loginUser = ucenterMemberService.getInfo();
        return R.ok().data("loginUser", loginUser);
    }

    /**
     * 更新会员信息
     */
    @ApiOperation("更新会员信息")
    @PostMapping("/updateInfo")
    public R updateInfo(@RequestBody UcenterMember ucenterMember) {
        if (ucenterMemberService.updateInfo(ucenterMember)) {
            return R.ok("更新成功");
        }
        return R.ok("更新失败");
    }

    /**
     * 通过验证码登录
     */
    @ApiOperation("通过验证码登录")
    @PostMapping("/loginByPhone/{phone}/{code}")
    public R loginByPhone(@PathVariable("phone") String phone, @PathVariable("code") String code) {

        String errorMsg = checkLoginParam(phone, code);
        if (StringUtils.isNotEmpty(errorMsg)) {
            return R.error(errorMsg);
        }
        String token = ucenterMemberService.loginByPhone(phone, code);
        if (StringUtils.isNotEmpty(token)) {
            return R.ok().data("token", token);
        }
        return R.error("登录失败");

    }

    /**
     * 检验登录的参数是否正确
     */
    private String checkLoginParam(String phone, String code) {

        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
            return "手机号和验证码不能为空";
        }
        return null;

    }
}

